﻿using MinecraftClient.Inventory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MinecraftClient.ChatBots.MyBots
{
    public class AutoDropTimed : ChatBot
    {
        private bool enable = false;
        private bool InventoryUpdated = false;

        private int InventoryId = 0;

        private int timer = 0;
        private int timerDefault = 2;

        private List<ItemType> itemList = new List<ItemType>();


        public override void Initialize()
        {
            itemList.Add(ItemType.GoldIngot);
            itemList.Add(ItemType.GoldenSword);
            itemList.Add(ItemType.RottenFlesh);
            RegisterChatBotCommand("adt", "AutoDropTimed", "on|off", CommandHandler);
        }

        public override void Update()
        {
            if (!enable || !InventoryUpdated) return;
            if (timer > 0)
            {
                timer--;
                return;
            }
            ThrowItems();
            timer = timerDefault;
        }

        public override void OnInventoryUpdate(int inventoryId)
        {
            InventoryUpdated = true;
        }

        public override void GetText(string text)
        {
            text = GetVerbatim(text);
            Regex on = new Regex(@"^\[.+\]<Gare_TH> adt on$");
            Regex off = new Regex(@"^\[.+\]<Gare_TH> adt off$");
            Regex add = new Regex(@"^\[.+\]<Gare_TH> adt add (.+)$");
            Regex time = new Regex(@"^\[.+\]<Gare_TH> adt time (.+)$");
            Regex unload = new Regex(@"^\[.+\]<Gare_TH> adt unload$");

            if (on.IsMatch(text))
            {
                ScriptOn();
                return;
            }

            if (off.IsMatch(text))
            {
                ScriptOff();
                return;
            }

            if (unload.IsMatch(text))
            {
                SendText("Unloaded");
                UnloadBot();
            }

            if (add.IsMatch(text))
            {
                string itemS = add.Match(text).Groups[1].Value;
                AddItem(itemS);
                return;
            }

            if (time.IsMatch(text))
            {
                try
                {
                    var cd = int.Parse(time.Match(text).Groups[1].Value);
                    SetCoolDown(cd);
                }
                catch (Exception e)
                {
                    SendText("Invalid integer.");
                }
                
            }
        }

        private string CommandHandler(string cmd, string[] args)
        {
            if (args.Length > 0)
            {
                switch (args[0].ToLower())
                {
                    case "on":
                        ScriptOn();
                        LogToConsole("on");
                        return "";
                    case "off":
                        ScriptOff();
                        LogToConsole("off");
                        return "";
                }
            }
            return "";
        }

        private void ScriptOn()
        {
            enable = true;
            timer = 0;
            //SendText("AutoDropTimed on.");
        }

        private void ScriptOff()
        {
            enable = false;
            timer = 0;
            //SendText("AutoDropTimed off.");
        }

        private void SetCoolDown(int time)
        {
            timerDefault = time;
            SendText("Set cooldown to " + time);
        }

        private void AddItem(string itemS)
        {
            ItemType item;
            if (Enum.TryParse(itemS, true, out item) && !itemList.Contains(item))
            {
                itemList.Add(item);
                SendText("Added " + item + " into item list.");
                return;
            }
            SendText("Wrong item type or already in the item list.");
        }

        private void ThrowItems()
        {
            var Inventory = GetInventories()[0];
            var Items = Inventory.Items.ToDictionary(key => key.Key, key => key.Value);

            foreach (var item in Items)
            {
                // skip crafting slot
                if (item.Key == 0) 
                    continue;
                if (itemList.Contains(item.Value.Type))
                {
                    LogToConsole("Throw " + item.Value.Type);
                    WindowAction(InventoryId, item.Key, WindowActionType.DropItemStack);
                }
            }
            InventoryUpdated = false;
        }
        
        
    }
}
